package org.jeecg.modules.zcgl.job;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.properties.config.JeecgPropertyConfig;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.zcgl.entity.LeaseContractInfo;
import org.jeecg.modules.zcgl.enums.LeaseContractStatusEnum;
import org.jeecg.modules.zcgl.enums.YesNoEnum;
import org.jeecg.modules.zcgl.service.ILeaseContractInfoService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 合同到期提醒
 */
@Slf4j
public class ExpirationContractJob implements Job {

    @Autowired
    private ILeaseContractInfoService leaseContractInfoService;
    @Autowired
    private JeecgPropertyConfig jeecgPropertyConfig;
    @Autowired
    private ISysBaseAPI baseAPI;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        long start = System.currentTimeMillis();
        log.info("定时合同到期提醒开始：" + start);
        try {
            Date date = DateUtils.nextDay(jeecgPropertyConfig.getReminderExpirationContract());
            QueryWrapper<LeaseContractInfo> queryWrapper = new QueryWrapper<>();
            LeaseContractInfo leaseContract = new LeaseContractInfo();
            leaseContract.setIzExpirationReminder(YesNoEnum.NO.getStatus());
            queryWrapper.lambda().in(LeaseContractInfo::getStatus, LeaseContractStatusEnum.TAKE_EFFECT.getStatus(), LeaseContractStatusEnum.RENEW.getStatus());
            queryWrapper.apply("DATE(rent_end_time) <= DATE('" + DateUtils.getDate(date, DateUtils.DATE_FORMAT) + "')");
            queryWrapper.setEntity(leaseContract);
            List<LeaseContractInfo> leaseContractInfoList = leaseContractInfoService.list(queryWrapper);
            leaseContractInfoList.stream().forEach(contractInfo -> {
                try {
                    String json = new ObjectMapper().writeValueAsString(contractInfo);
                    Map<String, String> newMap = JSONObject.parseObject(json, Map.class);
                    baseAPI.sendSysAnnouncement("系统", Arrays.asList(contractInfo.getCreateBy()), "合同到期提醒", newMap,
                            "expirationContract"/*, SysAnnmentTypeEnum.CONTRACT.getType(), contractInfo.getId()*/);
                    contractInfo.setIzExpirationReminder(YesNoEnum.YES.getStatus());
                    leaseContractInfoService.updateById(contractInfo);
                } catch (Exception e) {
                    log.error(contractInfo.getContractNum() + "合同到期提醒失败！" + e.getMessage());
                }
            });
        } catch (Exception e) {
            log.error("合同到期提醒失败！" + e.getMessage());
        }
        long end = System.currentTimeMillis();
        log.info("定时合同到期提醒结束：" + end);
        log.info("定时合同到期提醒耗时：" + (end - start) / 1000 + "S");
    }
}
